import Koa from 'koa'
import { createRouter } from 'koa-decorator-helpers'
import catchError from '~/middlewares/catchError'
import requestLog from '~/middlewares/request-log'
import { Api } from '~/routes/api'
import { Authorize } from '~/routes/authorize'
import { Callback } from '~/routes/callback'

export function createApp() {
    const app = new Koa({ proxy: true })

    app.use(requestLog())
    app.use(catchError())

    // app.use(
    //     koaBody({
    //         includeUnparsed: true,
    //     }),
    // )

    const router = createRouter({
        controllers: [Api, Authorize, Callback],
        prefix: process.env.ROUTES_PREFIX,
    })

    app.use(router.middleware()).use(router.allowedMethods())

    return app
}
